package com.gcd.gym.api;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcd.gym.model.Member;
import com.gcd.gym.model.form.CourseOfMember;
import com.gcd.gym.service.MemberService;
import com.gcd.gym.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping(value = "/api/v1/member",produces = MediaType.APPLICATION_JSON_VALUE)
public class MemberApi {
    private MemberService memberService;

    @Autowired
    public void setMemberService(MemberService memberService) {
        this.memberService = memberService;
    }

    @GetMapping
    public ResponseEntity<JsonResult <?>> findMember(@RequestParam(defaultValue = "1") Integer pageNo,
                                                     @RequestParam(defaultValue = "5") Integer pageSize,
                                                     Member member) {
        Page<Member> page=new Page<>(pageNo,pageSize);
        page=memberService.findMember(page,member);
        return ResponseEntity.ok(JsonResult.success(page));
    }

    //修改入会状态
    @PutMapping("/changeVip")
    public ResponseEntity<JsonResult<?>> deleteMembersById(@RequestBody Integer[] ids) {
        List<Integer> member= Arrays.asList(ids);
        int count=memberService.deleteMembersById(member);
        if (count!=ids.length) {
            return ResponseEntity.ok(JsonResult.fail("删除失败，后台异常"));
        } else {
            return ResponseEntity.ok(JsonResult.success(count));
        }

    }


    @PostMapping
    public ResponseEntity<JsonResult<?>> addMember(@RequestBody Member member) {
        boolean success= memberService.addMember(member);
        if(success) {
            return ResponseEntity.ok(JsonResult.success(success));
        } else {
            return ResponseEntity.ok(JsonResult.fail("添加会员失败"));
        }
    }

    @PutMapping
    public ResponseEntity<JsonResult<?>> editMember(@RequestBody Member member) {
        boolean success= memberService.editMember(member);
        if(success) {
            return ResponseEntity.ok(JsonResult.success(success));
        } else {
            return ResponseEntity.ok(JsonResult.fail("修改会员失败"));
        }
    }

    //会员的课程列表
    @GetMapping("/courseOfMember")
    public ResponseEntity<JsonResult<?>> courseOfMember(@RequestParam Integer memberId ,@RequestParam(required = false) Integer status) {
        List<CourseOfMember> courseOfMember= memberService.searchMembersCourse(memberId,status);
        return ResponseEntity.ok(JsonResult.success(courseOfMember));
    }

}
